package com.bana.fvs.protocol;

import com.bana.fvs.code.ControllerWorkMode;
import com.bana.fvs.model.Controller;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * 设置控制器工作参数
 */
public class SetControllerConfig {

    public int getId()
    {
        return 0x03;
    }


    public byte[] toBuffer(int kzqbsm, Controller controller) throws IOException {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        dos.writeByte(0x81);
        dos.writeByte(kzqbsm);
        dos.writeByte(getId());
        dos.writeByte(controller.getWdkz());
        if(controller.getWdkz()== ControllerWorkMode.FlashRed||
                controller.getWdkz()== ControllerWorkMode.FlashYellow||
                controller.getWdkz()== ControllerWorkMode.Detection)
        {
            dos.writeByte(controller.getWdld());
            dos.writeByte(controller.getLdsc()%128);
            dos.writeByte(controller.getLdsc()/128);
            dos.writeByte(controller.getMdsc()%128);
            dos.writeByte(controller.getMdsc()/128);
            dos.writeByte(controller.getHdys());
            dos.writeByte(controller.getMqys());
            dos.writeByte(controller.getHsjsdzsl());
        }

        dos.writeByte(0xFF);
        byte[] buffer= baos.toByteArray();
        dos.close();
        baos.close();
        return buffer;
    }

    public boolean toResult(byte[] buffer)
    {
        return buffer.length==4;
    }
}
